PgVector Store
π Fun with PgVectorStore, OpenAI, and Ollama in Spring AIβ
So, youβve decided to dive into the magical world of Spring AI, Postgres, and PgVectorStore? Buckle up! Weβre about to turn your database into a vector-storing, AI-boosted, RAG-ready beast! π¦Ύπ₯
ποΈ Step 1: Installing the Postgres PgVector Databaseβ
PgVector is like that cool kid in the database world who can store vectors and find similar ones in a snap! Letβs get it up and running.
π³ 1.1. Using Dockerβ
Why complicate things when Docker makes life easy? Run this:
docker run -it --rm --name postgres -p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e VECTOR_STORE_TYPE=pgVector \
pgvector/pgvector:pg16
Or, if youβre fancy and use Docker Compose, hereβs your setup:
services:
postgres:
image: pgvector/pgvector:pg16
container_name: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
VECTOR_STORE_TYPE: pgVector
π¨ Heads up! If you already have Postgres running, donβt forget to enable the extensions vector
, hstore
, and uuid-ossp
.
βοΈ Step 2: Configuring PgVector with Spring AIβ
Spring AI does the heavy lifting for us! Just set:
spring.ai.vectorstore.pgvector.initialize-schema=true
And it will magically run this schema setup:
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS vector_store (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
content text,
metadata json,
embedding vector(1536)
);
CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);
π‘ Pro Tip: You can customize the index type, distance type, and dimensions via application.properties
!
π€ Step 3: Configuring PgVectorStore with OpenAI Embedding Modelsβ
Want to make your app super smart with OpenAI? Let's do this! π§
3.1. Add Dependenciesβ
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
3.2. Properties Configurationβ
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.model=gpt-4
spring.ai.openai.embedding.model=text-embedding-ada-002
3.3. Demo App - Let's Test Itβ
@SpringBootApplication
public class App implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
private final VectorStore vectorStore;
public App(VectorStore vectorStore) {
this.vectorStore = vectorStore;
}
@Override
public void run(String... args) {
List<Document> documents = List.of(
new Document("Java is a high-level programming language."),
new Document("It is used for enterprise apps and Android development."),
new Document("Java has strong typing and automatic memory management.")
);
vectorStore.add(documents);
List<Document> results = vectorStore
.similaritySearch(SearchRequest.query("programming language").withTopK(1));
results.forEach(doc -> System.out.println(doc.getContent()));
}
}
π Output:
Java is a high-level programming language.
π€― Step 4: Configuring PgVectorStore with Ollama Embedding Modelsβ
Wanna run LLMs locally? Meet Ollama! π‘π¦
4.1. Run Ollama with Dockerβ
docker run -d --gpus all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
Now download & run a model:
ollama run mistral
4.2. Add Dependenciesβ
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
4.3. Properties Configurationβ
spring.ai.ollama.embedding.options.model=mistral
spring.ai.ollama.chat.options.model=mistral
4.4. Demo App - Ollama in Actionβ
Same as OpenAI, but powered by Mistral! β‘
π¨ Step 5: "Oops, Too Many Dimensions!"β
Error: column cannot have more than 2000 dimensions
Why? PgVector canβt handle >2000 dimensions for certain index types.
Fix Optionsβ
- Use a different vector store (like ChromDB).
- Set
index-type=none
(β οΈ Only for dev, NOT for production!). - Reduce dimensions under 2000.
Example:
spring.ai.vectorstore.pgvector.index-type=none # Dev only!
spring.ai.vectorstore.pgvector.dimensions=4096
π― Summary - What We Achieved Todayβ
- β Installed Postgres with PgVector extension.
- β Configured Spring AI for automatic setup.
- β Connected PgVector with OpenAI & Ollama.
- β Built a cool Spring Boot app to store & retrieve embeddings!
- β Solved the 2000-dimension error like a boss. π
π Happy Learning & Happy Coding! π